#!/bin/bash

# Database Console Backend Startup Script

echo "🚀 Starting Database Console Backend..."

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js (v16 or higher)"
    exit 1
fi

# Check if npm is installed
if ! command -v npm &> /dev/null; then
    echo "❌ npm is not installed. Please install npm"
    exit 1
fi

# Check if we're in the server directory
if [ ! -f "package.json" ]; then
    echo "❌ package.json not found. Please run this script from the server directory"
    exit 1
fi

# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
    echo "📦 Installing dependencies..."
    npm install
    if [ $? -ne 0 ]; then
        echo "❌ Failed to install dependencies"
        exit 1
    fi
fi

# Check if .env file exists
if [ ! -f ".env" ]; then
    echo "⚠️  .env file not found. Using default environment variables"
    echo "💡 Consider creating a .env file for custom configuration"
fi

# Display server information
echo ""
echo "📊 Database Console Backend"
echo "🌐 Server will be available at: http://localhost:3001"
echo "🔍 Health check endpoint: http://localhost:3001/health"
echo "📚 API documentation: http://localhost:3001/api"
echo ""

# Check if development dependencies are available
if command -v nodemon &> /dev/null && [ -f "node_modules/.bin/nodemon" ]; then
    echo "🔥 Starting development server with nodemon..."
    npm run dev
else
    echo "🏃 Starting production server..."
    npm start
fi